#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;

void traverse(const string &dirname) 
{
    vector<string> vec;
    DIR* dir;
    struct dirent* direntptr;
    dir = opendir(dirname.c_str());//打开目录
    if (!dir) 
    {
        cout << "this" << dirname.c_str() << " is nullptr " << endl;
        return;
    } 

    while ((direntptr = readdir(dir)) != nullptr) 
    {
        //把当前目录和上一级目录都去掉，避免死循环
        if(strncmp(direntptr->d_name, ".", 1) != 0 
           && strncmp(direntptr->d_name, "..", 2) != 0) 
        {
            vec.push_back(dirname+ "/" + direntptr->d_name);
            cout << dirname << "/" << direntptr->d_name << endl; 
        }
    }

    cout << "===================" << endl;
    for(auto &elem : vec)
    {
        cout << elem << endl;
    }
    closedir(dir);
}

int main(int argc, char *argv[])
{
    traverse("art");
    return 0;
}

